/**
 * 
 */
package hichecker.ui.components.custom_table.column_class;


/**
 *
 */
public class Grade implements Comparable<Grade> {
	
	private float grade;
	
	@Override
	public int compareTo(Grade o) {
		return Float.compare(this.grade,o.grade);
	}
	
	@Override
	public String toString() {
		return "" + grade;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Grade))
			return false;
		return ((Grade) obj).grade == this.grade;
	}
	
	public static float roundGrade(Grade grade) {
		// TODO
		return 1;
	}
	
	public static boolean isGrade(String input) {
		try {
			parseGrade(input);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
	
	public static float parseGrade(String input) {
		input = input.trim().replace(",", ".");
		// Empty input means no value set
		if (input.length() == 0)
			return -1;
		float grade = Float.parseFloat(input);
		int fst = (int) grade;
		int snd = (int) ((grade * 10) % 10);
		if (((fst == 4 || fst == 5) && snd == 0) ||
	       (fst <= 3 && fst >= 1 && (snd == 0 || snd == 3 || snd == 7))) {
			return fst + (snd /10f);
		} else {
			if (fst < 0)
				return -1;
			throw new NumberFormatException();
		}
	}

}
